import 'dart:ui';
import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:znmm/UI/utils/sharedpre_utils.dart';
import 'package:znmm/generated/l10n.dart';

part 'app_language_state.dart';

//多语言
class AppLanguageCubit extends Cubit<AppLanguageState> {
  AppLanguageCubit(Locale locale) : super(AppLanguageInitial(locale)) {
    loadLanguage();
  }

  //切换语言
  void _changMyLang(Locale locale) {
    String strLanguage = locale.languageCode;
    print("strLanguage:$strLanguage");
    if (strLanguage == "zh") {
      S.load(const Locale('zh', 'CN'));
    } else {
      S.load(const Locale('en', 'US'));
    }
  }

  Locale _locale(String languageCode) {
    switch (languageCode) {
      case 'en':
        return const Locale('en', 'US');
      case 'zh':
        return const Locale('zh', 'CN');
      default:
        return const Locale('en', 'US');
    }
  }

  //-------默认语言-------在这里修改
  void loadLanguage() {
    String languageCode =
        SharedPrefUtil().getData(SharedKey.strLangKey) ?? 'en'; //默认语言
    languageCode = "en"; //这里强制设置默认语言
    // UserInfoManage().iLanguage = 0;
    // if (languageCode == "en") {
    //   UserInfoManage().iLanguage = 1;
    // }

    final selectedLanguage = _locale(languageCode);

    _changMyLang(selectedLanguage);
    emit(AppLanguageUpdate(selectedLanguage));
  }

  void changeLanguage(String value) {
    // UserInfoManage().iLanguage = 0;
    // if (value == "en") {
    //   UserInfoManage().iLanguage = 1;
    // }

    SharedPrefUtil().setData(SharedKey.strLangKey, value);
    Locale mlocae = _locale(value);

    _changMyLang(mlocae);
    emit(AppLanguageUpdate(mlocae));
  }
}
